package com.hongdee.base.imp.controller;

import com.hongdee.base.entity.ResultEntity;
import com.hongdee.base.imp.entity.TExcelScheme;
import com.hongdee.base.imp.entity.TExcelSchemeProperty;
import com.hongdee.base.imp.service.IExcelSchemePropertyService;
import com.hongdee.base.imp.service.IExcelSchemeService;
import com.hongdee.base.imp.vo.ExcelSchemeVo;
import com.hongdee.common.util.EntityUtils;
import com.hongdee.common.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 名  称:ExcelSchemeController
 * 类  型: class
 * 描  述: 用一句话描述类的作用
 * 路  径:com.hongdee.base.imp.controller
 * 作  者:My
 * 日  期:2017/2/15 14:01
 */
@RestController
@RequestMapping("/base/execl")
public class ExcelSchemeController {

    @Autowired
    private IExcelSchemeService excelSchemeService;

    @Autowired
    private IExcelSchemePropertyService excelSchemePropertyService;


    /**
     * 分页查询导入方案
     *
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param templateName 模板名称
     * @return
     * @throws Exception
     */
    @RequestMapping("/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestParam(name = "templateName", defaultValue = "") String templateName,
                    @RequestParam(name = "brand", defaultValue = "") String brand) throws Exception {
        Map map = excelSchemeService.page(start, limit, templateName,brand);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    /**
     * 上传导入模板信息
     * @param uid
     * @param token
     * @param request
     * @param schemeId
     * @param file
     * @return
     * @throws Exception
     */
    @RequestMapping("/upload/temp")
    @ResponseBody
    public Map upload(@RequestParam(name = "uid", defaultValue = "") String uid,
                      @RequestParam(name = "token", defaultValue = "") String token,
                      HttpServletRequest request,
                      @RequestParam(name = "schemeId", defaultValue = "") String schemeId,
                      @RequestParam(name = "upExecl", defaultValue = "upExecl") MultipartFile file) throws Exception {
        Map map =new HashMap();
        String path = request.getSession().getServletContext().getRealPath(".");
        path = path + File.separator + "upload";
        excelSchemeService.uploadTemp(file,path,schemeId);
        map.put("success",true);
        map.put("msg","上传成功");
        return map;
    }

    /**
     * 模板下载详情
     * @param uid
     * @param token
     * @param schemeId
     * @return
     * @throws Exception
     */
    @RequestMapping("/download/info")
    @ResponseBody
    public Map downloadInfo(@RequestParam(name = "uid", defaultValue = "") String uid,
                        @RequestParam(name = "token", defaultValue = "") String token,
                        @RequestParam(name = "schemeId", defaultValue = "") String schemeId) throws Exception {
        Map map=new HashMap();
        Map data=this.excelSchemeService.downLoadInfoBySchemeId(schemeId);
        if(data!=null) {
            if(data.size()>0){
                map.put("data", data);
                map.put("success", true);
                map.put("msg","查询成功!!");
            }else{
                map.put("data", "");
                map.put("success", false);
                map.put("msg","文件不存在!!");
            }
        }else{
            map.put("data", "");
            map.put("success", false);
            map.put("msg","文件不存在!!");
        }

        return map;
    }

    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody TExcelScheme data) throws Exception {
        EntityUtils.valid(data);
        excelSchemeService.saveOrUpdate(data);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("保存成功！");
        return res;
    }

    /**
     * 初始化导入方案属性
     *
     * @param uid
     * @param token
     * @param schemeId
     * @param dataEntityId
     * @return
     */
    @RequestMapping("/init")
    @ResponseBody
    public ResultEntity init(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "schemeId", defaultValue = "") String schemeId,
                             @RequestParam(name = "dataEntityId", defaultValue = "") String dataEntityId) throws Exception {
        // excelSchemeService.saveOrUpdate(data);
        excelSchemePropertyService.initPropertyInfo(schemeId, dataEntityId);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("初始化属性成功！");
        return res;
    }


    @RequestMapping("/props")
    @ResponseBody
    public Map props(@RequestParam(name = "uid", defaultValue = "") String uid,
                     @RequestParam(name = "token", defaultValue = "") String token,
                     @RequestParam(name = "start", defaultValue = "0") String start,
                     @RequestParam(name = "limit", defaultValue = "50") String limit,
                     @RequestParam(name = "schemeId", defaultValue = "") String schemeId) throws Exception {
        if (StringUtils.isEmpty(schemeId)) {
            throw new Exception("请选择导入方案!");
        }
        Map res = this.excelSchemePropertyService.page(start, limit, schemeId);
        res.put("success", true);
        res.put("msg", "分页查询成功！");
        return res;
    }


    /**
     * 上传 Execl
     *
     * @param uid
     * @param token
     * @param schemeId
     * @param file
     * @return
     * @throws Exception
     */
    @RequestMapping("/upload")
    @ResponseBody
    public Map excelImport(@RequestParam(name = "uid", defaultValue = "") String uid,
                                    @RequestParam(name = "token", defaultValue = "") String token,
                                    HttpServletRequest request,
                                    @RequestParam(name = "schemeId", defaultValue = "") String schemeId,
                                    @RequestParam(name = "upExecl", defaultValue = "upExecl") MultipartFile file) throws Exception {

        if (StringUtils.isEmpty(schemeId)) {
            throw new Exception("请选择方案");
        }
        String path = request.getSession().getServletContext().getRealPath(".");

        path = path + File.separator + "upload";
        final ExcelSchemeVo scheme = this.excelSchemeService.loadExcelSchemeInfo(schemeId);
        Map map = excelSchemeService.parseExcel(file, scheme, uid,path);
        return map;
    }

    /**
     * 启用
     *
     * @param uid
     * @param token
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/start")
    @ResponseBody
    public ResultEntity start(@RequestParam(name = "uid", defaultValue = "") String uid,
                              @RequestParam(name = "token", defaultValue = "") String token,
                              @RequestParam(name = "id", defaultValue = "0") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        excelSchemeService.update(id, "1");
        res.setSuccess(true);
        res.setMsg("启用成功");
        return res;
    }

    /**
     * 停用
     *
     * @param uid
     * @param token
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/stop")
    @ResponseBody
    public ResultEntity stop(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "0") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        excelSchemeService.update(id, "0");
        res.setSuccess(true);
        res.setMsg("停用成功！");
        return res;
    }


    /**
     * 保存属性
     *
     * @param uid
     * @param token
     * @param props
     * @return
     * @throws Exception
     */
    @RequestMapping("/saveProps")
    @ResponseBody
    public ResultEntity saveProps(
            @RequestParam(name = "uid", defaultValue = "") String uid,
            @RequestParam(name = "token", defaultValue = "") String token,
            @RequestBody TExcelSchemeProperty props) throws Exception {
        EntityUtils.valid(props);
        this.excelSchemePropertyService.saveOrUpdateProperty(props);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("保存属性成功！");
        return res;
    }

    /**
     * 导入模板下拉选择
     *
     * @param uid
     * @param token
     * @return
     * @throws Exception
     */
    @RequestMapping("/combo")
    @ResponseBody
    public List queryCombo(@RequestParam(name = "uid", defaultValue = "") String uid,
                           @RequestParam(name = "token", defaultValue = "") String token) throws Exception {
        return this.excelSchemeService.combo(uid);

    }
}
